<?php
require_once("BaseDAO.php");
require_once('QuestionDAO.php');
require_once("TestDetailDAO.php");
require_once("../Entities/Test.php");
require_once("../Entities/TestDetail.php");

class TestDAO extends BaseDAO
{
	function CreateTest($item)
	{
		$sql = "insert into Tests(Name ,Description, CreatedBy, CreationOn) values ('" . 
			$item->Name . "','" . $item->Description . "','" . $item->CreatedBy . "',NOW());";
		mysql_query($sql);
		$item->Id = mysql_insert_id();
		$detailDAO = new TestDetailDAO();
		for($i = 0 ; $i < count($item->Details); $i++)
		{
			$detail = $item->Details[$i];
			$detail->TestId = $item->Id;
			$detailDAO->CreateTestDetail($detail);
		}
		return $item;
	}
	
	function CreateDetailsFromCategory($categoryId, $quantity, $marks)
	{
		$questionDAO = new QuestionDAO();
		$questions = $questionDAO->GetQuestionsByCategoryId($categoryId);
		$quantity = $quantity <= count($questions) ? $quantity : count($questions); 
		$kq = array();
		for($i = 0; $i < $quantity; $i++)
		{
			$random = rand(0, $quantity - $i - 1);
			$detail = new TestDetail();
			$detail->QuestionId = $questions[$random]->Id;
			$detail->Marks = $marks;
			$kq[] = $detail;
			unset($questions[$random]);
			$questions = array_values($questions);
		}
		return $kq;
	}
	
	function GetAll()
	{
		$sql = "select * from Tests";
		$result = mysql_query($sql);
		$list = array();
		$detailDAO = new TestDetailDAO();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Test();
			$item->Id = $row["Id"];
			$item->Name = $row["Name"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			$item->Description = $row["Description"];
			$item->Details = $detailDAO->GetTestDetailByTestId($item->Id);
			$list[] = $item;
		}
		return $list;
	}
	
	function GetTests($page, $pageSize, &$totalPage)
	{
		$sqlTotalPage = "select * from Tests";
		$sql = "select * from Tests limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		$resultTotalPage = mysql_query($sqlTotalPage);
		$result = mysql_query($sql);
		$totalPage = ceil(mysql_num_rows($resultTotalPage) / $pageSize);
		$list = array();
		$detailDAO = new TestDetailDAO();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Test();
			$item->Id = $row["Id"];
			$item->Name = $row["Name"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			$item->Description = $row["Description"];
			$item->Details = $detailDAO->GetTestDetailByTestId($item->Id);
			$list[] = $item;
		}
		return $list;
	}
	
	function GetTestById($id)
	{
		$sql = "select * from Tests where Id='" . $id . "'";
		$result = mysql_query($sql);
		$detailDAO = new TestDetailDAO();
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$item = new Test();
			$item->Id = $row["Id"];
			$item->Name = $row["Name"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			$item->Description = $row["Description"];
			$item->Details = $detailDAO->GetTestDetailByTestId($item->Id);
			return $item;
		}
		return null;
	}
	
	function DeleteTest($id)
	{
		$detailDAO = new TestDetailDAO();
		$detailDAO->DeleteTestDetailByTestId($id);
		$sql = "delete from Tests where Id='" . $id . "';";
		mysql_query($sql);
	}
}

?>